package com.tgram.android_mapapi.map;

import android.content.Context;

import com.esri.android.map.MapView;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.MultiPoint;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.symbol.PictureMarkerSymbol;

/**
 * @className: MapUtil
 * @description: 地图常用工具类
 * @author terry_zz
 * @date 2013-6-7 下午5:10:26
 */
public class MapUtil {

	private static PictureMarkerSymbol mGreenMarkerSymbol;
	private static PictureMarkerSymbol mYellowMarkerSymbol;
	private static PictureMarkerSymbol mBlueMarkerSymbol;
	private static PictureMarkerSymbol mVioletMarkerSymbol;
	private static PictureMarkerSymbol mPinkMarkerSymbol;
	private static PictureMarkerSymbol mRedMarkerSymbol;
	
	/** 地图最大缩放级别 **/
	public static final int MAX_LEVEL = 8;
	/** 地图最小缩放级别 **/
	public static final int MIN_LEVEL = 1;

	public static Point getMapPoint(double x, double y, MapView map) {
		Point point = new Point(x, y);
		Point mapPoint = MapUtil.getMapPoint(point, map);
		return mapPoint;
	}

	/**
	 * 二维坐标转当前地图参考系的坐标点
	 * 
	 * @param point
	 * @param map
	 * @return
	 */
	public static Point getMapPoint(Point point, MapView map) {
		Point mapPoint = (Point) GeometryEngine.project(point,
				SpatialReference.create(4326), map.getSpatialReference());
		return mapPoint;
	}

	/**
	 * 地图参考系坐标点转二维坐标系的坐标点
	 * 
	 * @param point
	 * @param map
	 * @return
	 */
	public static Point getPoint(Point point, MapView map) {
		Point mapPoint = (Point) GeometryEngine.project(point,
				map.getSpatialReference(), SpatialReference.create(4326));
		return mapPoint;
	}

	/**
	 * 针对ArcGis地图的地图级别(注意，如果用于MapView.zoomToResolution()需要除以2)
	 * 
	 * @param level
	 *            内网测试地图的level
	 * @return
	 */
	public static double getResolution(int level) {
		switch (level) {
		case 0:
			return 0.00073324271651485824 / 2;
		case 1:
			return 0.00036662135825742912 / 2;
		case 2:
			return 0.00018331067912871456 / 2;
		case 3:
			return 9.165533956435728e-005 / 2;
		case 4:
			return 4.5827669783447685e-005 / 2;
		case 5:
			return 2.2913834891723843e-005 / 2;
		case 6:
			return 1.1456917444592874e-005 / 2;
		case 7:
			return 5.728458723565484e-006 / 2;
		case 8:
			return 2.8642293605136961e-006 / 2;
		default:
			return 2.2913834891723843e-005 / 2;
		}
	}

	/**
	 * 获取当前地图的缩放级别
	 * 
	 * @param mapView
	 * @return
	 */
	public static int getCurrentZoomLevel(MapView mapView) {
		double resolution = mapView.getResolution();
		if (resolution <= 2.8642293605136961e-006) {
			return 8;
		} else if (resolution <= 5.728458723565484e-006 / 2) {
			return 7;
		} else if (resolution <= 1.1456917444592874e-005 / 2) {
			return 6;
		} else if (resolution <= 2.2913834891723843e-005 / 2) {
			return 5;
		} else if (resolution <= 4.5827669783447685e-005 / 2) {
			return 4;
		} else if (resolution <= 9.165533956435728e-005 / 2) {
			return 3;
		} else if (resolution <= 0.00018331067912871456 / 2) {
			return 2;
		} else if (resolution <= 0.00036662135825742912 / 2) {
			return 1;
		} else {
			return 0;
		}
	}

	/**
	 * 针对ArcGis地图的地图级别(注意，如果用于MapView.zoomToResolution()需要除以2)
	 * 
	 * @param level
	 *            外网测试level
	 * @return
	 */
	/*
	 * public static double getResolution(int level) { switch (level) { case 0:
	 * return 156543.033928 / 2; case 1: return 78271.5169639999 / 2; case 2:
	 * return 39135.7584820001 / 2; case 3: return 19567.8792409999 / 2; case 4:
	 * return 9783.93962049996 / 2; case 5: return 4891.96981024998 / 2; case 6:
	 * return 2445.98490512499 / 2; case 7: return 1222.99245256249 / 2; case 8:
	 * return 611.49622628138 / 2; case 9: return 305.748113140558 / 2; case 10:
	 * return 152.874056570411 / 2; case 11: return 76.4370282850732 / 2; case
	 * 12: return 38.2185141425366 / 2; case 13: return 19.1092570712683 / 2;
	 * case 14: return 9.55462853563415 / 2; case 15: return 4.77731426794937 /
	 * 2; case 16: return 2.38865713397468 / 2; case 17: return 1.19432856685505
	 * / 2; case 18: return 0.597164283559817 / 2; case 19: return
	 * 0.298582141647617 / 2; default: return 305.748113140558 / 2; } }
	 */
	// private final double EARTH_RADIUS = 6378137.0;
	/**
	 * 两个gps点之间的距离
	 * 
	 * @param lat_a
	 * @param lng_a
	 * @param lat_b
	 * @param lng_b
	 * @return
	 */
	public static double gps2m(double lat_a, double lng_a, double lat_b,
			double lng_b) {
		double radLat1 = (lat_a * Math.PI / 180.0);
		double radLat2 = (lat_b * Math.PI / 180.0);
		double a = radLat1 - radLat2;
		double b = (lng_a - lng_b) * Math.PI / 180.0;
		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
				+ Math.cos(radLat1) * Math.cos(radLat2)
				* Math.pow(Math.sin(b / 2), 2)));
		s = s * 6378137.0;
		s = Math.round(s * 10000) / 10000;
		return s;
	}

	/**
	 * 将地图理解成一个平面，这个方法速度更快
	 * 
	 * @param lat_a
	 * @param lng_a
	 * @param lat_b
	 * @param lng_b
	 * @return
	 */
	public static double distanceBetweenPoints(double lat_a, double lng_a,
			double lat_b, double lng_b) {
		return (lat_a - lat_b) * (lat_a - lat_b) + (lng_a - lng_b)
				* (lng_a - lng_b);
	}

	/**
	 * 根据建筑的状态，获取对应的图片symbol
	 * 
	 * @param state
	 * @param context
	 * @return
	 */
	// public static PictureMarkerSymbol getPicSymbolByRealState(int state,
	// Context context) {
	// PictureMarkerSymbol _MarkerSymbol = null;
	// switch (state) {
	// case ModelAddress.FLAG_VISIT:
	// if (mGreenMarkerSymbol == null) {
	// mGreenMarkerSymbol = new
	// PictureMarkerSymbol(context.getResources().getDrawable(R.drawable.double_rhfac_location_green_big));
	// }
	// _MarkerSymbol = mGreenMarkerSymbol;
	// break;
	// case ModelAddress.FLAG_BUILD:
	// if (mYellowMarkerSymbol == null) {
	// mYellowMarkerSymbol = new
	// PictureMarkerSymbol(context.getResources().getDrawable(R.drawable.double_rhfac_location_red_big));
	// }
	// _MarkerSymbol = mYellowMarkerSymbol;
	// break;
	// case ModelAddress.FLAG_LABEL:
	// if (mBlueMarkerSymbol == null) {
	// mBlueMarkerSymbol = new
	// PictureMarkerSymbol(context.getResources().getDrawable(R.drawable.double_rhfac_location_blue_big));
	// }
	// _MarkerSymbol = mBlueMarkerSymbol;
	// break;
	// case ModelAddress.FLAG_NOT_LABEL:
	// if (mVioletMarkerSymbol == null) {
	// mVioletMarkerSymbol = new
	// PictureMarkerSymbol(context.getResources().getDrawable(R.drawable.double_location_violet));
	// }
	// _MarkerSymbol = mVioletMarkerSymbol;
	// break;
	// default:
	// break;
	// }
	// return _MarkerSymbol;
	// }

//	/**
//	 * 已访查
//	 * 
//	 * @param context
//	 * @return
//	 */
//	public static PictureMarkerSymbol getVisitSymbol(Context context) {
//		if (mGreenMarkerSymbol == null) {
//			mGreenMarkerSymbol = new PictureMarkerSymbol(context.getResources()
//					.getDrawable(R.drawable.double_rhfac_location_green_big));
//			// mGreenMarkerSymbol.setOffsetY(-20);
//		}
//		return mGreenMarkerSymbol;
//	}
//
//	/**
//	 * 已建房
//	 * 
//	 * @param context
//	 * @return
//	 */
//	public static PictureMarkerSymbol getBuildSymbol(Context context) {
//		if (mYellowMarkerSymbol == null) {
//			mYellowMarkerSymbol = new PictureMarkerSymbol(context
//					.getResources().getDrawable(
//							R.drawable.double_rhfac_location_yellow_big));
//		}
//		return mYellowMarkerSymbol;
//	}
//
//	/**
//	 * 已标注
//	 * 
//	 * @param context
//	 * @return
//	 */
//	public static PictureMarkerSymbol getLabelSymbol(Context context) {
//		if (mBlueMarkerSymbol == null) {
//			mBlueMarkerSymbol = new PictureMarkerSymbol(context.getResources()
//					.getDrawable(R.drawable.double_rhfac_location_blue_big));
//		}
//		return mBlueMarkerSymbol;
//	}
//
//	/**
//	 * 未标注
//	 * 
//	 * @param context
//	 * @return
//	 */
//	public static PictureMarkerSymbol getNotLabelSymbol(Context context) {
//		if (mVioletMarkerSymbol == null) {
//			mVioletMarkerSymbol = new PictureMarkerSymbol(context
//					.getResources().getDrawable(
//							R.drawable.double_rhfac_location_red_big));
//		}
//		return mVioletMarkerSymbol;
//	}

	/**
	 * 返回多点中，与点击最近的点
	 * 
	 * @param _MultiPoint
	 *            多点graphic
	 * @param _TapPoint
	 *            点击的点
	 * @return
	 */
	public static Point getTargetPointInMultiPoint(MultiPoint _MultiPoint,
			Point _TapPoint) {
		int _Size = _MultiPoint.getPointCount();
		double _MinM = -1, _Dis = 0.0;
		Point _NearestPoint = null;
		Point _Point = null;
		for (int i = 0; i < _Size; i++) {
			_Point = _MultiPoint.getPoint(i);
			_Dis = MapUtil.distanceBetweenPoints(_TapPoint.getY(),
					_TapPoint.getX(), _Point.getY(), _Point.getX());
			if (_MinM == -1) {
				_MinM = _Dis;
				// 第一个点作为最近点
				_NearestPoint = _Point;
				// _NearestPointStack.push(_Point);
			} else {
				if (_Dis < _MinM) {
					_MinM = _Dis;
					_NearestPoint = _Point;
				}
			}
		}
		return _NearestPoint;
	}

	/**
	 * 取得该点在multipoint中的位置
	 * 
	 * @param lon
	 * @param lat
	 * @param mPt
	 * @return
	 */
	public static int getIndexInMultiPoint(double lon, double lat,
			MultiPoint mPt) {
		int _Size = mPt.getPointCount();
		double _MinM = -1, _Dis = 0.0;
		int index = 0;
		for (int i = 0; i < _Size; i++) {
			Point _Point = mPt.getPoint(i);
			// LogUtil.d(TAG, "point x:" + _Point.getX() + ",point y:" +
			// _Point.getY());
			_Dis = MapUtil.distanceBetweenPoints(lon, lat, _Point.getY(),
					_Point.getX());
			if (_MinM == -1) {
				_MinM = _Dis;
			} else {
				if (_Dis < _MinM) {
					_MinM = _Dis;
					index = i;
				}
			}
		}
		return index;
	}

	
//	public static PictureMarkerSymbol getYBZWJFSymbol(Context context) {
//		if (mBlueMarkerSymbol == null) {
//			mBlueMarkerSymbol = new PictureMarkerSymbol(context.getResources()
//					.getDrawable(R.drawable.double_rhfac_location_blue_big));
//			// mGreenMarkerSymbol.setOffsetY(-20);
//		}
//		return mBlueMarkerSymbol;
//	}
//	
//	/**
//	 * 已建房未访查，粉色
//	 * 
//	 * @param context
//	 * @return
//	 */
//	public static PictureMarkerSymbol getYJFWFCSymbol(Context context) {
//		if (mPinkMarkerSymbol == null) {
//			mPinkMarkerSymbol = new PictureMarkerSymbol(context
//					.getResources().getDrawable(R.drawable.double_rhfac_location_pink_big));
//		}
//		return mPinkMarkerSymbol;
//	}
//	
//	/**
//	 * 已建房访查中，黄色
//	 * 
//	 * @param context
//	 * @return
//	 */
//	public static PictureMarkerSymbol getYJFFCZSymbol(Context context) {
//		if (mYellowMarkerSymbol == null) {
//			mYellowMarkerSymbol = new PictureMarkerSymbol(context.getResources()
//					.getDrawable(R.drawable.double_rhfac_location_yellow_big));
//		}
//		return mYellowMarkerSymbol;
//	}
//	
//	/**
//	 * 访查完成，绿色
//	 * 
//	 * @param context
//	 * @return
//	 */
//	public static PictureMarkerSymbol getFCWCSymbol(Context context) {
//		if (mGreenMarkerSymbol == null) {
//			mGreenMarkerSymbol = new PictureMarkerSymbol(context
//					.getResources().getDrawable(R.drawable.double_rhfac_location_green_big));
//		}
//		return mGreenMarkerSymbol;
//	}
//	
//	/**
//	 * 未标注，红色
//	 * 
//	 * @param context
//	 * @return
//	 */
//	public static PictureMarkerSymbol getWBZSymbol(Context context) {
//		if (mRedMarkerSymbol == null) {
//			mRedMarkerSymbol = new PictureMarkerSymbol(context
//					.getResources().getDrawable(R.drawable.double_rhfac_location_red_big));
//		}
//		return mRedMarkerSymbol;
//	}
}
